import { axios } from '@/utils';
import * as apis from './api';
import { Config } from './interface';

const request = async <T>(config: Config<T>) => {
  const { method = 'get', url, params } = config;

  try {
    const res = await axios({
      method,
      url,
      [method === 'get' ? 'params' : 'data']: params,
    });

    return {
      data: res.data,
      code: res.status,
    };
  } catch (error) {
    return {
      data: null,
      code: 1010,
    };
  }
};

// 获取app列表
export const getProjectList = async <T>(params?: T) => {
  return request<T>({ ...apis.getProjectList, params });
};

// 获取project列表
export const getGroupList = <T>(params?: T) => {
  return request<T>({ ...apis.getGroupList, params });
};

// 新增应用
export const addProject = <T>(params?: T) => {
  return request<T>({ ...apis.addProject, params });
};

// 删除应用
export const deleteProject = (id = '') => {
  apis.deleteProject.url = apis.deleteProject.url + '/' + id;
  return request({ ...apis.deleteProject });
};

// 获取迭代列表
export const getIterationList = <T>(params?: T) => {
  return request<T>({ ...apis.getIterationList, params });
};

// 新增迭代
export const addIteration = <T>(params?: T) => {
  return request<T>({ ...apis.addIteration, params });
};

// 删除迭代
export const deleteIteration = (id?: string) => {
  apis.deleteIteration.url = `${apis.deleteIteration.url}/${id || ''}`;
  return request({ ...apis.deleteIteration });
};

// 设置环境url
export const saveEnvLocation = <T>(params?: T) => {
  return request<T>({ ...apis.saveEnvLocation, params });
};
